//编写着色器修改底图样式
export default function modifyMap(viewer) {
  //获取地图的底图影像图层 0代表第一层
  let baseLayer = viewer.imageryLayers.get(0);
  //设置2个变量,用来判断是否进行颜色的翻转和过滤
  //设置颜色翻转
  baseLayer.invertColor = true;
  //设置颜色过滤 //[255,255,255]过滤后[255,105,155]
  baseLayer.filterRGB = [0, 50, 100];
  //修改底图着色器的代码
  const baseFragmentShader =
    viewer.scene.globe._surfaceShaderSet.baseFragmentShaderSource.sources;
  // console.log('baseFragmentShader', baseFragmentShader);

  //循环修改着色器
  for (let i = 0; i < baseFragmentShader.length; i++) {
    // console.log('i', baseFragmentShader[i]);
    const strS = 'color = czm_saturation(color, textureSaturation);\n#endif\n';
    let strT = 'color = czm_saturation(color, textureSaturation);\n#endif\n';
    //判断转换
    if (baseLayer.invertColor) {
      strT += `
        color.r = 1.0 - color.r;
        color.g = 1.0 - color.g;
        color.b = 1.0 - color.b;
      `;
    }

    if (baseLayer.filterRGB) {
      strT += `
        color.r = color.r*${baseLayer.filterRGB[0]}.0/225.0;
        color.g = color.g*${baseLayer.filterRGB[1]}.0/225.0;;
        color.b = color.b*${baseLayer.filterRGB[2]}.0/225.0;;
      `;
    }

    //替换
    baseFragmentShader[i] = baseFragmentShader[i].replace(strS, strT);
  }
}
